package com.store.pet.petstore.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.bean.UserBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SharedPreferencesUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.account_et)
    EditText accountEt;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.registered)
    TextView registered;
    @BindView(R.id.login)
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);

        //控件随着输入框上升
        View decorView = getWindow().getDecorView();
        View contentView = findViewById(Window.ID_ANDROID_CONTENT);
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener(decorView, contentView));
    }

    private ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView, final View contentView) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                decorView.getWindowVisibleDisplayFrame(r);

                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int diff = height - r.bottom;

                if (diff != 0) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, 0, 0, 0);
                    }
                }
            }
        };
    }

    @Override
    public void loadData() {
        if(SharedPreferencesUtil.getBeanFromSp(this,Constant.FILE_NAME,Constant.DATA) != null){
            Intent intent =  new Intent(LoginActivity.this,HomeActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @OnClick({R.id.registered, R.id.login})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.registered:
                intent.setClass(this,RegisteredActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                String username = accountEt.getText().toString();  //用户名
                String password = passwordEt.getText().toString();  //密码
                if(!username.equals("") && !password.equals("")){
                    Map<String,Object> map = new HashMap<>();
                    map.put("userphone",username);
                    map.put("userpsd",password);
                    //封装的okhttp请求，回调结果
                    OKHttpUtils.requestData(Constant.LOGIN, map, new OKHttpUtils.ServerCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            UserBean user = GsonUtils.parseUser(data);
                            if(user.getCode() == 100){
                                SharedPreferencesUtil.saveBean2Sp(LoginActivity.this,user,Constant.FILE_NAME,Constant.DATA);
                                intent.setClass(LoginActivity.this,HomeActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            ToastUtils.showShort(user.getMsg());
                        }

                        @Override
                        public void onFailed(String message) {
                            ToastUtils.showShort("登录失败");  //弹窗
                        }
                    });
                }else {
                    ToastUtils.showShort("请填写完整");
                }
//                if(username.equals("aaa") && password.equals("123")){
//                    startActivity(new Intent(LoginActivity.this,HomeActivity.class));
//                }
                break;
        }
    }
}
